package de.dopichaj.labrador.index.backend;

import java.io.DataOutput;
import java.io.IOException;

import de.dopichaj.labrador.util.io.BitOutputStream;
import de.dopichaj.labrador.util.io.ByteWriter;


public final class TermFrequencyOutputStream {

    private final BitOutputStream output;
    private int previousTermID = -1;
    private int previousDocumentID = -1;
    private final boolean compress;
    private final boolean writeTermID;
    
    public TermFrequencyOutputStream(final DataOutput output,
        final boolean compress, final boolean writeTermID) {
        
        this.output = new BitOutputStream(new ByteWriter(output, 2048));
        this.compress = compress;
        this.writeTermID = writeTermID;
    }

    public TermFrequencyOutputStream(final DataOutput output) {
        this(output, true, true);
    }

    public void writeTermFrequency(final TermFrequency freq) throws IOException {
        
        assert freq != null;
        assert freq.getDocumentID() >= 0;
        assert freq.getTermID() >= 0;
        assert freq.getFrequency() > 0;
        
        if (compress) {
            
            writeCompressed(freq);

        } else {
            if (writeTermID) {
                writeBig(freq.getTermID() + 1);
            }
            writeBig(freq.getDocumentID());
        }
        
        output.writeUnary(freq.getFrequency());
    }

    private void writeCompressed(final TermFrequency freq) throws IOException {
        
        final boolean startOfRun = freq.getTermID() != previousTermID;
        if (startOfRun) {

            if (writeTermID) {
                
                // write the start-of-run marker
                writeSmall(0);
                
                // if this is the first term ID or the ID is less than the
                // previous one, write the full ID
                if (previousTermID == -1 || freq.getTermID() < previousTermID) {
                    writeSmall(0);
                    writeBig(freq.getTermID() + 1);
                
                // else write the delta to the previous term ID
                } else {
                    writeSmall(freq.getTermID() - previousTermID + 1);
                }
            }

            // write the document ID
            writeBig(freq.getDocumentID());

            previousTermID = freq.getTermID();
            
        } else {
            
            assert previousDocumentID >= 0;
            if (writeTermID) {
                if (freq.getDocumentID() >= previousDocumentID) {
                    writeSmall(2 + freq.getDocumentID() - previousDocumentID);
                } else {
                    writeSmall(1);
                    writeBig(freq.getDocumentID());
                }
            } else {
                writeSmall(1 + freq.getDocumentID() - previousDocumentID);
            }
        }

        previousDocumentID = freq.getDocumentID();
    }
    
    private void writeSmall(int value) throws IOException {
        output.writeDelta(value + 1);
    }
    
    private void writeBig(int value) throws IOException {
        output.writeVariableLengthInt(value);
    }

    public void terminateRun() throws IOException {
        if (compress) {
            writeSmall(0);
            if (writeTermID) {
                writeSmall(0);
                writeBig(0);
            }
        } else {
            writeBig(0);
        }
        previousDocumentID = -1;
        previousTermID = -1;
    }

    public void flush() throws IOException {
        output.flush();
    }
}
/*
Copyright (c) 2007 Philipp Dopichaj

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/